package com.yihr.yrd;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.math.BigDecimal;

public class App {

    public static void main(String[] args) {

        System.out.println("hello");

        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/spring-*.xml");

        EhCacheCacheManager cacheCacheManager = applicationContext.getBean("cacheManager", EhCacheCacheManager.class);
        CacheManager cacheManager = cacheCacheManager.getCacheManager();




        testEhcatch(cacheManager);

    }

    public static void testEhcatch(CacheManager cacheManager) {

//        // 1. 创建缓存管理器
//        CacheManager cacheManager = CacheManager.create();

        String path = cacheManager.getDiskStorePathManager().getFile("ehcache77").getAbsolutePath();

        System.out.println(path);
        // 2. 获取缓存对象
        Cache cache = cacheManager.getCache("HelloWorldCache");

        // 3. 创建元素
        Element element = new Element("key1", "value1");

        // 4. 将元素添加到缓存
        cache.put(element);

        // 5. 获取缓存
        Element value = cache.get("key1");
        System.out.println(value);
        System.out.println(value.getObjectValue());

        // 6. 删除元素
        cache.remove("key1");

        BigDecimal dog = new BigDecimal(123.34);
        Element element2 = new Element("taidi", dog);
        cache.put(element2);
        Element value2 = cache.get("taidi");
        BigDecimal dog2 = (BigDecimal) value2.getObjectValue();
        System.out.println(dog2);

        System.out.println(cache.getSize());

        Element ele = new Element("key", "value");
        Cache test2 = cacheManager.getCache("HelloWorldCache");
        test2.put(ele);
        Element ele3 = test2.get("key");
        System.out.println(ele == ele3);  //true

        // 7. 刷新缓存
        cache.flush();


        // 8. 关闭缓存管理器
        cacheManager.shutdown();


    }


}
